#include "info.h"
#include "re.h"

INFO * OneGetInfo() {

   FILE *fp;
   INFO *info;
   RE *re;
   char line[512], *buffer;

   fp = fopen("onebot.conf","r");
   if(fp == NULL) {
      printf("Impossivel abrir onebot.conf\n");
      exit(0);
   }

   re = (RE *) malloc(sizeof(RE));

   info = (INFO *) malloc(sizeof(INFO));
   info->httpbind=0;

   while(fgets(line, 512, fp) != NULL) {

      if(matcher("port=\"([^\"]+)\"",line, re)) {

         buffer = re_copy(re,1,line);
         info->port = atoi(buffer);
         free(buffer);

      } else if(matcher("ident=\"([^\"]+)\"",line, re)) {

         info->ident = re_copy(re,1,line);

      } else if(matcher("server=\"([^\"]+)\"",line, re)) {

         info->server = re_copy(re,1,line);

      } else if(matcher("http=\"([0-9]+)\"",line, re)) {

         buffer = re_copy(re,1,line);
         info->httpbind=atoi(buffer);
         free(buffer);

      } else if(matcher("name=\"([^\"]+)\"",line, re)) {

         info->name = re_copy(re,1,line);

      } else if(matcher("nick=\"([^\"]+)\"",line, re)) {

         info->nick = re_copy(re,1,line);

      } else if(matcher("password=\"([^\"]+)\"",line, re)) {

         info->password = re_copy(re,1,line);

      } else if(matcher("modules=\"([^\"]+)\"",line, re)) {

         buffer = re_copy(re,1,line);
         explode(',',buffer, info->modules);
         free(buffer);

      }

   }

   fclose(fp);

   info->alive = 1;

   return info;

}
